﻿// Author: Jithu Jose, Email: jithu89@hotmail.it
// 
// This file is part of Schedule.
//
// Schedule is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Schedule is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with Schedule.  If not, see <http://www.gnu.org/licenses/>.

namespace JJware.Schedule
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Text;
    using System.Windows.Forms;

    public partial class FrmAddNewItem : Form
    {
        #region Fields
        
        private static FrmMain _mainForm;
        private DateTime _time;

        #endregion Fields

        #region Constructors

        /// <summary>
        /// 
        /// </summary>
        /// <param name="time">Initial time to show on the form.</param>
        /// <param name="mainForm">A reference to the main form.</param>
        public FrmAddNewItem(DateTime time, FrmMain mainForm)
        {
            InitializeComponent();
            Initialize(time);

            _mainForm = mainForm;
        }

        #endregion Constructors

        #region Methods

        /// <summary>
        /// Initialize some internal variables.
        /// </summary>
        /// <param name="time">Initial time to show on the form.</param>
        private void Initialize(DateTime time)
        {
            _time = time;
            nudHour.Value = _time.Hour;
            nudMinute.Value = _time.Minute;
            nudSecond.Value = _time.Second;
        }

        /// <summary>
        /// Event Handler: Invoked when the user click the add item button after filling out the path
        /// and time with valid values.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddItem_Click(object sender, EventArgs e)
        {
            DateTime selectedTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month,
                DateTime.Now.Day,(int)nudHour.Value, (int)nudMinute.Value, (int)nudSecond.Value);

            if (DateTime.Compare(selectedTime, DateTime.Now) > 0)
            {
                ItemInfo itemInfo = new ItemInfo(txtItem.Text, selectedTime);
                _mainForm.UpdateItems(itemInfo);
                Close();
            }
            else
            {
                MessageBox.Show("Invalid time!");
            }
        }

        /// <summary>
        /// Event Handler: Invoked when the user click on the Path text box to show the
        /// OpenFileDialog window.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                if (File.Exists(ofd.FileName))
                {
                    txtItem.Text = ofd.FileName;
                }
            }
        }

        #endregion Methods
    }
}